﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using cs499.Database;

namespace cs499.Locations
{
    public partial class AddRoom : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["AddRoomLabeltext"] != null && IsPostBack)
            {
                AddRoomLabel.Text = (string)HttpContext.Current.Session["AddRoomLabeltext"];
                //OKButton.Enabled = false;
            }
            else if (IsPostBack)
            {
                AddRoomLabel.Text = "";
            }
            else
            {
                AddRoomLabel.Text = "";
            }
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Locations/ListLocations.aspx");
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            LinkButton locationsMenuButton = (LinkButton)Master.FindControl("LoginView").FindControl("LocationsMenuButton");
            locationsMenuButton.CssClass = "selected";

            if (AddRoomLabel.Text != "") return;
            if (HttpContext.Current.Session["CurrentDepartmentId"] != null)
            {
                int CurrentDepartmentId = (int)HttpContext.Current.Session["CurrentDepartmentId"];
                cs499.Database.Department CurrentDepartment = ListOfDepartment.GetDepartment(ID: CurrentDepartmentId);
                AddRoomLabel.Text = "Input information for new Room in The " + CurrentDepartment.Name + " department";
            }
            else
            {
                Response.Redirect("~/Locations/ListLocations.aspx");
            }
        }

        protected void OKButton_Click(object sender, EventArgs e)
        {
            String strroomcapacity = inputroomcapacity.Text;
            String roomnumber = inputroomnumber.Text;
            int roomcapacity;
            if (roomnumber == "")
            {
                String errmsg = "Room Number Cannot be blank";
                if (AddRoomLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["AddRoomLabeltext"] = errmsg;
                    Server.Transfer("~/Locations/AddRoom.aspx", true);
                }
                return;
            }
            if (!int.TryParse(strroomcapacity, out roomcapacity))
            {
                String errmsg = "Room Capacity must be a number greater than 0";
                if (AddRoomLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["AddRoomLabeltext"] = errmsg;
                    Server.Transfer("~/Locations/AddRoom.aspx", true);
                }
                return;
            }
            Department curdep;
            if (HttpContext.Current.Session["CurrentDepartmentId"] == null)
            {
                DropDownList seldep = (DropDownList)Page.Master.FindControl("LoginViewForDepartmentSelection_DepartmentSelection");

                curdep = ListOfDepartment.GetDepartment(Name: seldep.Text);
                if (curdep == null) HttpContext.Current.Session["CurrentDepartmentId"] = null;
                else HttpContext.Current.Session["CurrentDepartmentId"] = curdep.Id;
                HttpContext.Current.Session["CurrentDepartmentselectedindex"] = seldep.SelectedIndex;
            }
            else
            {
                int idtocheckfor = (int)HttpContext.Current.Session["CurrentDepartmentId"];
                curdep = ListOfDepartment.GetDepartment(ID: idtocheckfor);
            }
            Building curbuilding = curdep.GetBuilding(strID: DropDownListofbuildings.SelectedValue);
            if (curbuilding == null)
            {
                String errmsg = "No Building selected";
                if (AddRoomLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["AddRoomLabeltext"] = errmsg;
                    Server.Transfer("~/Locations/AddRoom.aspx", true);
                }
                return;
            }

            if (curbuilding.CreateRoom(roomnumber, roomcapacity) == null)
            {
                String errmsg = "Unknown Error while creating Room";
                if (AddRoomLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["AddRoomLabeltext"] = errmsg;
                    Server.Transfer("~/Locations/AddRoom.aspx", true);
                }
                return;
            }
            HttpContext.Current.Session["AddRoomLabeltext"] = "";
            AddRoomLabel.Text = "Success";
            Response.Redirect("~/Locations/ListLocations.aspx?action=success");

        }
    }
}